package com.ruoyi.user.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.user.domain.vo.UserLoginVO;
import com.ruoyi.user.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.user.domain.TbUser;
import com.ruoyi.user.service.ITbUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户Controller
 * 
 * @author wuxiao
 * @date 2024-08-19
 */
@RestController
@RequestMapping("/user")
public class TbUserController extends BaseController
{
    @Autowired
    private ITbUserService tbUserService;


    /**
     * @description //TODO 上传图片的方法
     * @author chenyulong
     * @date 2024/8/21 15:04
     * @param file
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/uploadOss")
    public AjaxResult uploadOss(MultipartFile file) throws IOException {
        String upload = OssUtil.Oss(file);
        return AjaxResult.success("图片上传成功",upload);
    }


    /**
     * @description //TODO 用于用户登陆的接口
     * @author chenyulong
     * @date 2024/8/19 20:22
     * @param userLoginVO
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/loginByPassWord")
    public AjaxResult loginByPassWord(@RequestBody UserLoginVO userLoginVO){
        return tbUserService.loginByPassWord(userLoginVO);
    }


    /**
     * @description //TODO 账号验证码登录
     * @author chenyulong
     * @date 2024/8/19 22:32
     * @param userLoginVO
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/loginByCode")
    public AjaxResult loginByCode(@RequestBody UserLoginVO userLoginVO){
        return tbUserService.loginByCode(userLoginVO);
    }


    /**
     * @description //TODO 根据账号获取验证码
     * @author chenyulong
     * @date 2024/8/19 22:24
     * @param account
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/getCode/{account}")
    public AjaxResult getCode(@PathVariable("account")String account){
        return tbUserService.getCode(account);
    }

    @PostMapping("/register")
    public AjaxResult register(@RequestBody TbUser tbUser){
        return tbUserService.register(tbUser);
    }


    /**
     * 查询用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TbUser tbUser)
    {
        startPage();
        List<TbUser> list = tbUserService.selectTbUserList(tbUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbUser tbUser)
    {
        List<TbUser> list = tbUserService.selectTbUserList(tbUser);
        ExcelUtil<TbUser> util = new ExcelUtil<TbUser>(TbUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(tbUserService.selectTbUserByUserId(userId));
    }

    /**
     * 新增用户
     */
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbUser tbUser)
    {
        return toAjax(tbUserService.insertTbUser(tbUser));
    }

    /**
     * 修改用户
     */
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbUser tbUser)
    {
        return toAjax(tbUserService.updateTbUser(tbUser));
    }

    /**
     * 删除用户
     */
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(tbUserService.deleteTbUserByUserIds(userIds));
    }
}
